﻿var me = this,
    vm = me.lookupViewModel();

Ext.require([
    'PAI.ux.window.selection.SingleSelectionWin',
    'PAI.ux.panel.selection.EmpSelectionComp'
], function () {
    var win = Ext.create({
        title: 'Choose Line Manager',
        xtype: 'singleselectionwin',
        modal: true,
        isMulti: false,
        selectType: 'empselection',
        autoShow: true,
        enableSearch: false,
        columnsConfig: [{
            dataIndex: 'ext_jlrLineManager',
            text: Glb_lang('Line Manager')
        }, {
            dataIndex: 'ext_subDept',
            text: Glb_lang('Department')
        }, {
            dataIndex: 'ext_jlrLineManagerCdSId',
            text: Glb_lang('Line Manager CDSID')
        }],
        dockedItems: [{
            xtype: 'toolbar',
            dock: 'top',
            items: ['->', {
                xtype: 'textfield',
                name: 'searchField',
                triggers: {
                    clear: { //清空按钮
                        weight: 0,
                        cls: Ext.baseCSSPrefix + 'form-clear-trigger',
                        hidden: true,
                        handler: function () {
                            var me = win.down('grid');

                            Ext.Array.each(me.activeFilter, function (filter) {
                                me.store.getFilters().remove(filter);
                            });
                            me.activeFilter = null;
                            this.setValue('');
                            this.getTrigger('clear').hide();
                        },
                        scope: 'this'
                    },
                    search: { //选择按钮
                        weight: 1,
                        cls: Ext.baseCSSPrefix + 'form-search-trigger',
                        handler: function () {
                            var fieldValue = win.down('textfield[name=searchField]').getValue();
                            var me = win.down('grid');

                            var activeFilter = [{
                                property: 'ext_jlrLineManager',
                                anyMatch: true,
                                value: fieldValue
                            }, {
                                property: 'ext_jlrLineManagerCdSId',
                                anyMatch: true,
                                value: fieldValue
                            }];
                            this.getTrigger('clear').show();
                            me.store.getFilters().add(activeFilter);
                            me.store.getProxy().filterOperator = 'or';
                            me.store.getProxy().filterOperatorCustom = me.filterOperatorCustom;
                            me.activeFilter = activeFilter;
                        },
                        scope: 'this'
                    }
                },

            }]
        }],
        listeners: {
            'selectionchange': function (win, selections) {
                if (!Ext.isEmpty(selections)) {

                    var empNo = [];
                    var empList = [];
                    var emailList = [];
                    Ext.each(selections, function (item) {
                        empNo.push(item.ext_jlrLineManagerCdSId);
                        empList.push(Ext.String.format('{0}({1})', item.ext_jlrLineManager, item.ext_jlrLineManagerCdSId));
                        emailList.push(item.email);
                    });
                    vm.set('NebulogyForm.lineManagerCDSID', empNo.join(','));
                    vm.set('NebulogyForm.agencyInfo.reportTo', empList.join(','));
                    vm.set('NebulogyForm.agencyInfo.reportToEmail', emailList.join(';'));
                }
            }
        }
    });

    win.down('grid').getColumns()[1].hide();
}, me);
